﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;
using TracNghiemTinHocDaiCuong.BIZ;
 

namespace TracNghiemTinHocDaiCuong
{
    public partial class ThemNguoiDung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime dtNgayDangKi = DateTime.Now;
            string ngay = "", thang = "";
            ngay = (dtNgayDangKi.Day < 10) ? ("0" + dtNgayDangKi.Day) : dtNgayDangKi.Day.ToString();
            thang = (dtNgayDangKi.Month < 10) ? ("0" + dtNgayDangKi.Month) : dtNgayDangKi.Month.ToString();
            txtNgayDangKi.Text = ngay + "/" + thang + "/" + DateTime.Now.Year;
        }

        protected void btnThemNguoiDung_Click(object sender, EventArgs e)
        {
            NguoiDung nguoiDungMoi = new NguoiDung();
            nguoiDungMoi.TenTaiKhoan = txtTenTaiKhoan.Text;
            nguoiDungMoi.MatKhau = txtMatKhau.Text;
            nguoiDungMoi.HoVaTen = txtHoTen.Text;
            nguoiDungMoi.NgaySinh = Methods.ConvertDateTime(ConvertDateTimeToString(txtNgaySinh.Text));
            nguoiDungMoi.Email = txtEmail.Text;
            nguoiDungMoi.GioiTinh = rblGioiTinh.SelectedValue;
            nguoiDungMoi.DienThoai = txtDienThoai.Text;
            nguoiDungMoi.DiaChi = txtDiaChi.Text;
            nguoiDungMoi.NgayDangKi = Methods.ConvertDateTime(txtNgayDangKi.Text);
            NguoiDungBIZ.Them(nguoiDungMoi);
            lblThongBao.Text = "Bạn đã thêm " + txtTenTaiKhoan.Text + " thành công";
        }
        private string ConvertDateTimeToString(string exp)
        {
            if (string.IsNullOrEmpty(exp))
                return "04/01/2013";
            string[] time = exp.Split('/');
            if (Methods.ConvertNumber(time[0]) > 23)
                return "04/01/2013";
            if (Methods.ConvertNumber(time[1]) > 59)
                return "04/01/2013";
            return string.Format(CultureInfo.CurrentCulture, "{0}/{1}/{2}",
                time[1], time[0], time[2]);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuanLyNguoiDung.aspx");
        }

        protected void btnXoaTuKhoa_Click(object sender, EventArgs e)
        {
            txtTenTaiKhoan.Text = "";
            txtMatKhau.Text = "";
            txtHoTen.Text = "";
            txtEmail.Text = "";
            txtDienThoai.Text = "";
            txtDiaChi.Text = "";
        }
    }
}